package com.cxq.leetcode;

/**
 * Created by cxq on 2020/8/1.
 */
public class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
        this.next = null;
    }

    //增加节点
    public ListNode add(ListNode newNode){
        ListNode node = this;
        while(node.next!=null){
            node = node.next;
        }
        node.next = newNode;
        return this;
    }


    //遍历链表
    public void show(){
        ListNode node = this;
        while(node.next!=null){
            System.out.println(node.val);
            node = node.next;
        }
        System.out.println(node.val);
    }

    //逆序链表
    public ListNode resverListNode(){
        ListNode newNode = null;
        ListNode head = this;
        while(head==null){
            //备份head。next
            ListNode next = head.next;
            //更新head.next
            head.next = newNode;
            //移动newNode
            newNode = head;
            //遍历链表
            head = next;
        }
        return newNode;
    }
}
